<?php

namespace app\common\model\mysql;

use think\Model;

class WarehouseSpaceProduct extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'p_warehouse_space_product';

    /**
     * 查询仓位下商品列表
     * @param $where
     * @param $field
     * @return mixed
     */
    public function getNormalWarehouseSpaceProductList($where, $field){
        $order = [
            "a.warehouse_space_product_id" =>"asc",
        ];
        $result = $this->alias('a')->join('p_warehouse b', 'a.warehouse_id = b.warehouse_id')->join('p_warehouse_space c', 'a.warehouse_space_id = c.warehouse_space_id')->join('p_product d', 'a.product_id = d.product_id')->where($where)->field($field)->order($order)->select();
        return $result->toArray();
    }

    /**
     * 查询仓位下单个商品
     * @param $where
     * @param $field
     * @return mixed
     */
    public function getNormalWarehouseSpaceProductFind($where, $field){
        $result = $this->alias('a')->join('p_warehouse b', 'a.warehouse_id = b.warehouse_id')->join('p_warehouse_space c', 'a.warehouse_space_id = c.warehouse_space_id')->where($where)->field($field)->find();
        return $result ? $result->toArray() : [];
    }

    /**
     * 仓位下数量减 X
     * @param $where
     * @param $field
     * @param int $count
     * @return false
     */
    public function saveDecWarehouseSpaceProductField($where, $field, int $count = 1){
        if(empty($field)){
            return false;
        }
        return $this->where($where)->dec($field,$count)->update();
    }

    /**
     * 仓位下数量加X
     * @param $where
     * @param $field
     * @param int $count
     * @return false
     */
    public function saveInWarehouseSpaceProductField($where, $field, int $count = 1){
        if(empty($field)){
            return false;
        }
        return $this->where($where)->inc($field,$count)->update();
    }

    /**
     * 新增仓位下数量
     * @param $data
     * @return mixed
     * @throws \think\Exception
     */
    public function addWarehouseSpaceProductData($data){
        $this->save($data);
        return $this->id;
    }
}
